<?php
require_once('security.php');

  // Retrieve keywords for a specified page
  if (isset($_POST['submit1'])) { // Handle the form
    // Convert case and trim ends
    $page = trim(strtolower($_POST['thispage']));
    // Strip out extra spaces in search string
    $page = preg_replace("/[ ]+/", " ", $page);
    // Check something was entered
    $number = str_word_count($page);
    if ($number < 1) {
      $problem = true;
      $response = "You didn't enter a page name.\n";
    } else {
      $problem = false;
    }
    // If something was entered...
    if (!$problem) {
      $filename = "../search/index.txt";
      $fp = fopen($filename, "r") or die("Couldn't open $filename.");
      // Loop through all the lines
      while (!feof($fp)) {
        $line = fgets($fp, 1024);
        // Check each line for search term
        if (preg_match("/\/$page\b/", $line)) { // \b = boundary
          $result = true;
          // Convert line string into array using | as separator
          $lineArray = explode("|", $line);

          // Get the page title from $lineArray
          $pagetitle = $lineArray[1];

          // Slice off after 2nd element to new array
          $keywordArray = array_slice($lineArray, 2);
          // Convert back to comma-separated string
          $keywordString = implode(", ", $keywordArray);
        } // End of match conditional
      } // End of while loop
      if (!$result) {
        $response = "Sorry, no keywords were found for <b>$page</b>.<br />Perhaps there is no page with that name.";
      }
    }
  }

  // Edit keywords
  if (isset($_POST['submit2'])) { // Handle the form

    // Retrieve data and format variables

    // (1) Get posted values
    $page = $_POST['thispage'];
    // Target string to be replaced
    $existingKeywords = $_POST['existingKeywords']; // From hidden field
    // Replacement string
    $keywords = $_POST['keywords'];

    // (2) Convert "comma+space" to "|"
    $existingKeywords = str_replace(", ", "|", $existingKeywords);
    $existingKeywords = trim($existingKeywords);
    $keywords = str_replace(", ", "|", $keywords);
    $keywords = trim($keywords);

    // (3) Strip out extra spaces in keywords string
    $newKeywords = preg_replace("/[ ]{2,}/", "", $keywords);

    // (4) Set the path to the file
    $filename = "../search/index.txt";

    // (5) Get the file contents as an array
    $fileArray = file($filename);

    // Get the Page Title for $page
    foreach ($fileArray as $val) {
      // Check each $val for $page
      if (preg_match("/\/$page\b/", $val)) { // \b = boundary
        // Convert val string into array
        $valArray = explode("|", $val);
        // Get second element
        $pagetitle = $valArray[1];
      } // End of match conditional
    }

    // Do the update

    // Open the file for writing
    $fp = fopen($filename, "w") or die("Couldn't open $filename.");

    // Convert $fileArray to string for writing
    $contents = implode($fileArray);

    // Update keywords: in $contents, replace revised substring
    $contents2 = trim(str_replace($existingKeywords, $newKeywords, $contents)); // Not working

    // Write new content
    fwrite($fp, $contents2);
    fclose($fp);
    $response = "The keywords were updated.";
  }

  // Delete page
  if (isset($_POST['submit3'])) { // Handle the form
    // Set the page
    $page = $thispage;
    // Convert strings to index.txt format
    $existingKeywords = str_replace(", ", "|", $existingKeywords);
    // Add the page and page title
    $toDelete = "/$page|$pagetitle|$existingKeywords";
    // Set the path to the file
    $filename = "../search/index.txt";
    // Get the file contents as an array
    $fileArray = file($filename);
    // Convert array to string
    $contents = implode($fileArray);
    // Write new, omitting deleted string
    $contents = str_replace($toDelete, "", $contents);
    $contents = trim($contents);
    // Open the file for writing
    $fp = fopen($filename, "w") or die("Couldn't open $filename.");
    // Write new content
    fwrite($fp, $contents);
    fclose($fp);
    $response = "The page keywords were deleted.";
  }

?>

<?php $thisPage = "searchindex"; ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Edit search index</title>
<meta name="robots" content="noindex,nofollow" />
<?php require_once('theme/admintheme.php');?>
</head>
<body>

<div id="wrap">


<?php include_once('navigation.php'); ?>

<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
Existing page name (no file suffix) - enter name and press "Get keywords" (home page = <b>index</b>)<br />
<input type="text" name="thispage" id="thispage" size="40" value="<?php if (isset($_POST['submit1'])) { echo $_POST['thispage']; } else if (isset($_POST['submit2'])) { echo $thispage; } ?>" maxlength="60" /><br />
Page title desired for search results listing<br />
<input type="text" name="pagetitle" id="pagetitle" size="40" value="<?php if ((isset($_POST['submit1'])) || (isset($_POST['submit2']))) { echo $pagetitle; } ?>" maxlength="60" /><br />
Keywords and phrases (separated by comma+space)<br />
<textarea class="textarea" name="keywords" cols="70" rows="3"><?php if (isset($_POST['submit1'])) { echo stripslashes($keywordString); } else if (isset($_POST['submit2'])) { echo stripslashes($_POST['keywords']); } ?></textarea><br />
<input type="hidden" name="existingKeywords" value="<?php if (isset($_POST['submit1'])) { echo $keywordString; } else if (isset($_POST['submit2'])) { echo ($_POST['existingKeywords']); } else if (isset($_POST['submit3'])) { echo $existingKeywords; } ?>" />
<input type="submit" name="submit1" value="Get keywords" /> <input type="submit" name="submit2" value="Update keywords" /> <input type="submit" name="submit3" value="Delete page row" />
</form>

<?php echo $response; ?>

<h3>Existing search index</h3>

<?php

  // Set the path to the file
  $filename = "../search/index.txt";
  // Get the file contents as an array
  $fileArray = file($filename);
  // Loop through the array
  foreach ($fileArray as $val) {
    $val = str_replace("|", ", ", $val);
    echo "&#8226; $val<br />";
  }

?>

</div>

</body>
</html>
